<?php

class HeaderScripts
{
	public static $_jsFiles  = array();
	public static $_cssFiles = array();
	
	public function __construct()
	{
		
	}
	
	public function prepareJsLibrary($scripts)
	{
		for ($i = 0; $i < count($scripts); $i++) {
			$scripts[$i] = ((substr($scripts[$i], 0, 1) == '/') ? 'public/assets/' : '/public/assets/') . $scripts[$i];
		}
		
		return $scripts;
	}
	
	public function prepareScripts($scripts)
	{
		for ($i = 0; $i < count($scripts); $i++) {
			$scripts[$i] = ((substr($scripts[$i], 0, 1) == '/') ? 'public/js/' : '/public/js/') . $scripts[$i];
		}
		
		return $scripts;
	}
	
	public function prepareStyles($styles)
	{
		for ($i = 0; $i < count($styles); $i++) {
			$styles[$i] = ((substr($styles[$i], 0, 1) == '/') ? 'public/css/' : '/public/css/') . $styles[$i];
		}
		
		return $styles;
	}
	
	public function addScripts($scripts, $position = 'left')
	{
		if (!is_array($scripts)) {
			$scripts = array(0 => $scripts);
		}
		
		if ($position == 'left') {
			self::$_jsFiles = array_merge(self::$_jsFiles, self::prepareScripts($scripts));
		} else if ($position == 'right') {
			self::$_jsFiles = array_merge(self::prepareScripts($scripts), self::$_jsFiles);
		}
	}
	
	public function addJsLibrary($scripts, $position = 'left')
	{
		if ($position == 'left') {
			self::$_jsFiles = array_merge(self::$_jsFiles, self::prepareJsLibrary($scripts));
		} else if ($position == 'right') {
			self::$_jsFiles = array_merge(self::prepareJsLibrary($scripts), self::$_jsFiles);
		}
	}
	
	public function addStyles($styles, $position = 'left')
	{
		if (!is_array($styles)) {
			$styles = array(0 => $styles);
		}
		
		if ($position == 'left') {
			self::$_cssFiles = array_merge(self::$_cssFiles, self::prepareStyles($styles));
		} else if ($position == 'right') {
			self::$_cssFiles = array_merge(self::prepareStyles($styles), self::$_cssFiles);
		}
	}
	
	public function render()
	{
		$html = '';
		
		foreach (self::$_cssFiles as $style) {
			$html .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . $style  . "\" />\r\n";
		}
		
		foreach (self::$_jsFiles as $script) {
			$html .= "<script type=\"text/javascript\" src=\"" . $script . "\"></script>\r\n";
		}
		
		return $html; 
	}
}